package com.link.base.task;

import com.link.base.base.questionnairesurvey.model.Survey;
import com.link.base.base.questionnairesurvey.service.SurveyService;
import com.link.base.core.basic.config.xxljob.BaseLinkJobHandler;
import com.link.core.util.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * xxx
 * 
 * @author zhenghongda
 * @date 2019/07/09 21:06
 */
@JobHandler(value = "taskQuestionnaire")
@Component
public class TaskQuestionnaire extends BaseLinkJobHandler {

    @Resource
    private SurveyService surveyService;

    private static final Logger logger = LogManager.getLogger(TaskQuestionnaire.class);

    @Override
    public ReturnT<String> execute(String params) throws Exception {
        XxlJobLogger.log("XXL-JOB, Start.");
        try {
            super.setLogin(params);
            long now = (new Date()).getTime();
            Survey query = new Survey();
            query.setState("Published");
            List<Survey> surveys = surveyService.queryByExamplePage(query);
            if (surveys != null) {
                for (Survey survey : surveys) {
                    Date startTime = DateUtil.attemptStringToDate(survey.getEndTime());
                    long end = startTime.getTime();
                    XxlJobLogger.log("XXL-JOB,Now..." + now);
                    XxlJobLogger.log("XXL-JOB,End..." + end);
                    if (now >= end) {
                        survey.setState("Disabled");
                        surveyService.update(survey);
                    }
                }
            }
        } catch (Exception e) {
            XxlJobLogger.log(e.getMessage());
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }
}
